package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Add_remove_film extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testAddRemove() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    WebElement usernameField = driver.findElement(By.id("username"));
	usernameField.clear();
    usernameField.sendKeys("admin");
    WebElement passwordField = driver.findElement(By.name("password"));
	passwordField.clear();
    passwordField.sendKeys("admin");
    driver.findElement(By.name("submit")).click();
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.name("name"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    WebElement submitField = driver.findElement(By.id("submit"));
	submitField.click();
    // Warning: waitForTextPresent may require manual changes
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*This field is required[\\s\\S]*$")) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("label.error"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    WebElement NameField = driver.findElement(By.name("name"));
	NameField.clear();
    NameField.sendKeys("Fox training");
    WebElement akaField = driver.findElement(By.name("aka"));
	akaField.clear();
    akaField.sendKeys("Very few people know that foxes very bright and curious animals.");
    WebElement yearField = driver.findElement(By.name("year"));
	yearField.clear();
    yearField.sendKeys("1984");
    WebElement durationField = driver.findElement(By.name("duration"));
	durationField.clear();
    durationField.sendKeys("120");
    WebElement subtitlesField = driver.findElement(By.name("subtitles"));
	subtitlesField.clear();
    subtitlesField.sendKeys("fox language");
    WebElement langField = driver.findElement(By.id("text_languages_0"));
	langField.clear();
    langField.sendKeys("gav-gav");
    WebElement audioField = driver.findElement(By.name("audio"));
	audioField.clear();
    audioField.sendKeys("forest sounds");
    WebElement countryField = driver.findElement(By.name("country"));
	countryField.clear();
    countryField.sendKeys("Boundless forest open spaces");
    WebElement castField = driver.findElement(By.name("cast"));
	castField.clear();
    castField.sendKeys("Real fox ^^");
    submitField.click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("img[alt=\"Remove\"]"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.xpath("//a[contains(text(),'Home')]")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("q"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    WebElement searchField = driver.findElement(By.id("q"));
	searchField.clear();
    searchField.sendKeys("Fox" + Keys.RETURN);
 //   searchField.sendKeys(Keys.ENTER);
 //     for (int second = 0;; second++) {
 //   	if (second >= 60) fail("timeout");
 //   	try { if (!isElementPresent(By.xpath("//div[@class=\"movie_box\"]//*[@class='title' and not(contains(text(),'Fox'))]"))) break; } catch (Exception e) {}
 //   	Thread.sleep(1000);
 //   }

 //   for (int second = 0;; second++) {
 //   	if (second >= 60) fail("timeout");
//    	try { if (isElementPresent(By.xpath("//div[@class=\"movie_box\"]//*[@class='title' and not(contains(text(),'Fox'))]"))) break; } catch (Exception e) {}
//    	Thread.sleep(1000);
//    }

    driver.findElement(By.xpath("//div[contains(text(), 'Fox training')]")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("img[alt=\"Remove\"]"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
    assertTrue(Pattern.compile(".*").matcher(closeAlertAndGetItsText()).find());
    // Warning: waitForTextNotPresent may require manual changes
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (!driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*my first name[\\s\\S]*$")) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.xpath("//a[contains(@href, '/php4dvd/?logout')]")).click();
    assertTrue(Pattern.compile(".*").matcher(closeAlertAndGetItsText()).find());
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("username"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
